package javaCode.chapter07.object_;

public class ToString_ {
    public static void main(String[] args) {

        /*
        Object的ToString() 源码
        1、getClass().getName() 类的全类名(类名+包名);
        2、Integer.toHexString(hashCode()) 将对象的hashCode转换为 16进制的字符串
        public String toString() {
            return getClass().getName() + "@" + Integer.toHexString(hashCode());
        }
         */

        Monster monster = new Monster("小妖怪", "巡山", 2000);
        System.out.println(monster.toString());

        System.out.println("==当直接输出一个对象时，默认输出的就是toString方法");
        System.out.println(monster);

        String name = new String("汪汪汪");
        System.out.println(name); //String属性 重写了toString
    }
}

//Monster 妖怪
class Monster{
    private String name;
    private String job;
    private double sal;

    public Monster(String name, String job, double sal) {
        this.name = name;
        this.job = job;
        this.sal = sal;
    }

    //重写toString方法，输出对象的属性
    //使用快捷键即可，alt+insert -> toString()

    @Override
    public String toString() { //重写后，一般是输出对象的属性，当然程序员也可以自行修改
        return "Monster{" +
                "name='" + name + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                '}';
    }
}
